package com.junjiao.util.java.email;

import java.util.Properties;

import javax.mail.Session;

/**
 * @author wgz
 * 
 */
public class EmailSsessionFactory {

	/**
	 * @param ServceHost
	 *            邮件服务器
	 * @param protocol
	 *            发邮件协议
	 * @param protoPort
	 *            邮件服务器端口
	 * @param sslPort
	 *            是否ssl发邮件
	 * @param userName
	 *            发送人邮箱
	 * @param password
	 *            发送人邮箱密码
	 * @return
	 * @throws Exception
	 */
	private static Session initSession(String ServceHost, String protocol,
			int protoPort, int sslPort, String userName, String password)
			throws Exception {

		Properties props = System.getProperties();
		EmailAutherticator auth = null;

		if (protocol != null) {
			props.put("mail.transpost.protocol", protocol);
		} else {
			props.put("mail.transpost.protocol", "smtp");
		}

		if (ServceHost != null) {
			props.put("mail.smtp.host", ServceHost);
		} else {
			throw new Exception("host is null!");
		}

		if (protoPort != 0) {
			props.put("mail.smpt.port", protoPort);
		} else {
			props.put("mail.smpt.port", 25);
		}
		props.put("mail.smtp.auth", "true");

		if (sslPort != 0) {
			props.put("mail.smtp.socketFactory.class",
					"javax.net.ssl.SSLSocketFactory");
			props.put("mail.smtp.socketFactory.fallback", "false");
			props.put("mail.smtp.socketFactory.port", sslPort);
		}

		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.debug", "true");

		if (userName != null && password != null) {
			auth = new EmailAutherticator(userName, password);
		} else {
			throw new Exception("userName is null or password is null!");
		}

		Session session = Session.getInstance(props, auth);
		session.setDebug(true);

		return session;
	}

	/**
	 * 
	 * @param ServceHost
	 *            邮件服务器
	 * @param userName
	 *            发送人邮箱
	 * @param password
	 *            发送人邮箱密码
	 * @return
	 * @throws Exception
	 */
	public static Session getSession(String ServceHost, String userName,
			String password) throws Exception {

		try {
			return initSession(ServceHost, null, 0, 0, userName, password);

		} catch (Exception e) {
			throw new Exception(e.getCause());
		}
	}

	public static Session getSession(String ServceHost, String protocol,
			int protoPort, String userName, String password) throws Exception {

		try {
			return initSession(ServceHost, protocol, protoPort, 0, userName,
					password);

		} catch (Exception e) {
			throw new Exception(e);
		}
	}

	public static Session getSession(String ServceHost, int sslPort,
			String userName, String password) throws Exception {

		try {
			return initSession(ServceHost, null, 0, sslPort, userName, password);

		} catch (Exception e) {
			throw new Exception(e);
		}
	}

	public static Session getSession(String ServceHost, String protocol,
			int protoPort, int sslPort, String userName, String password)
			throws Exception {

		try {
			return initSession(ServceHost, protocol, protoPort, sslPort,
					userName, password);

		} catch (Exception e) {
			throw new Exception(e);
		}
	}

	public static void main(String args[]) {

		String ServceHost = "smtp.189.cn";
		String userName = "13316099335";
		String password = "19820714";
		try {
			Session se = EmailSsessionFactory.getSession(ServceHost, userName,
					password);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
